﻿using System;
using System.Threading;

namespace msgExchange
{
    public abstract class TaskThread
    {
        protected string name_ = "";
        protected bool isInterrupted_ = false;
        public Thread th_;

        public TaskThread() { }

        public TaskThread(string name)
        {
            name_ = name;
        }

        public string Name
        {
            get { return name_; }
        }

        public bool Status
        {
            get { return th_.IsAlive; }
        }

        public bool Start()
        {
            try
            {
                th_ = new Thread(new ThreadStart(ThreadStartDO));
                th_.Start();
            }
            catch
            {
                return false;
            }
            return true;
        }

        private void ThreadStartDO()
        {
            try
            {
                ThreadRountine();
            }
            catch (Exception)
            {
            }
        }

        public void Stop()
        {
            isInterrupted_ = true;
            th_.Interrupt();
        }

        protected abstract void ThreadRountine();

    }
}